
// src/main/java/com/xxxy/ljh/myapplication/DaoImpl/AssociationDaoImpl.java
package com.xxxy.ljh.myapplication.DaoImpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xxxy.ljh.myapplication.Dao.AssociationDao;
import com.xxxy.ljh.myapplication.dphelper.sqliteDBHelper;
import com.xxxy.ljh.myapplication.model.Association;


import java.util.ArrayList;
import java.util.List;

/**
 * 社团数据访问接口实现类，提供社团信息的增删改查等数据库操作功能。
 */
public class AssociationDaoImpl implements AssociationDao {

    private Context mContext;
    private SQLiteDatabase mDatabase;

    /**
     * 构造方法，初始化数据库连接
     *
     * @param context 应用上下文对象，用于创建数据库帮助类实例
     */
    public AssociationDaoImpl(Context context) {
        mContext = context;
        mDatabase = new sqliteDBHelper(mContext).getWritableDatabase();
    }

    /**
     * 创建新的社团记录
     *
     * @param association 要插入的社团对象，包含社团的基本信息
     * @return 返回新插入记录的行号，-1表示插入失败
     */
    @Override
    public long createAssociation(Association association) {
        ContentValues values = new ContentValues();
        values.put("association_name", association.getName());
        values.put("founder_id", association.getFounderId());
        values.put("category", association.getCategory());
        values.put("description", association.getDescription());
        values.put("logo", association.getLogo());
        values.put("contact_phone", association.getContactPhone());
        values.put("contact_email", association.getContactEmail());
        values.put("audit_status", association.getAuditStatus());
        values.put("status", association.getStatus());
        values.put("member_count", association.getMemberCount());

        long result = mDatabase.insert("association", null, values);
        return result;
    }

    /**
     * 根据社团ID查询社团信息
     *
     * @param associationId 社团唯一标识符
     * @return 查询到的社团对象，未找到则返回null
     */
    @Override
    public Association getAssociationById(int associationId) {
        String selection = "association_id = ?";
        String[] selectionArgs = {String.valueOf(associationId)};
        Cursor cursor = null;
        try {
            cursor = mDatabase.query("association", null, selection, selectionArgs, null, null, null);

            Association association = null;
            if (cursor.moveToFirst()) {
                association = parseAssociationFromCursor(cursor);
            }
            return association;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 获取所有社团列表
     *
     * @return 包含所有社团信息的列表
     */
    @Override
    public List<Association> getAllAssociations() {
        Cursor cursor = null;
        try {
            cursor = mDatabase.query("association", null, null, null, null, null, null);
            List<Association> associations = new ArrayList<>();

            while (cursor.moveToNext()) {
                Association association = parseAssociationFromCursor(cursor);
                associations.add(association);
            }
            return associations;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 获取审核通过且状态正常的社团列表
     *
     * @return 审核通过的社团列表
     */
    @Override
    public List<Association> getApprovedAssociations() {
        String selection = "audit_status = ? AND status = ?";
        String[] selectionArgs = {"1", "1"};
        Cursor cursor = null;
        try {
            cursor = mDatabase.query("association", null, selection, selectionArgs, null, null, null);
            List<Association> associations = new ArrayList<>();

            while (cursor.moveToNext()) {
                Association association = parseAssociationFromCursor(cursor);
                associations.add(association);
            }
            return associations;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 更新社团信息
     *
     * @param association 包含更新后信息的社团对象
     * @return 更新成功返回true，否则返回false
     */
    @Override
    public boolean updateAssociation(Association association) {
        ContentValues values = new ContentValues();
        values.put("association_name", association.getName());
        values.put("founder_id", association.getFounderId());
        values.put("category", association.getCategory());
        values.put("description", association.getDescription());
        values.put("logo", association.getLogo());
        values.put("contact_phone", association.getContactPhone());
        values.put("contact_email", association.getContactEmail());
        values.put("audit_status", association.getAuditStatus());
        values.put("audit_time", association.getAuditTime());
        values.put("audit_admin_id", association.getAuditAdminId());
        values.put("audit_remark", association.getAuditRemark());
        values.put("update_time", association.getUpdateTime());
        values.put("status", association.getStatus());
        values.put("member_count", association.getMemberCount());

        String whereClause = "association_id = ?";
        String[] whereArgs = {String.valueOf(association.getAssociationId())};

        int result = mDatabase.update("association", values, whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 删除指定ID的社团记录
     *
     * @param associationId 要删除的社团ID
     * @return 删除成功返回true，否则返回false
     */
    @Override
    public boolean deleteAssociation(int associationId) {
        String whereClause = "association_id = ?";
        String[] whereArgs = {String.valueOf(associationId)};

        int result = mDatabase.delete("association", whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 根据创始人ID获取其创建的所有社团
     *
     * @param founderId 创始人用户ID
     * @return 该用户创建的社团列表
     */
    @Override
    public List<Association> getAssociationsByFounderId(int founderId) {
        String selection = "founder_id = ?";
        String[] selectionArgs = {String.valueOf(founderId)};
        Cursor cursor = null;
        try {
            cursor = mDatabase.query("association", null, selection, selectionArgs, null, null, null);
            List<Association> associations = new ArrayList<>();

            while (cursor.moveToNext()) {
                Association association = parseAssociationFromCursor(cursor);
                associations.add(association);
            }
            return associations;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * 从 Cursor 中解析 association 对象
     *
     * @param cursor 数据库游标，指向当前要解析的数据行
     * @return 解析后的社团对象
     */
    private Association parseAssociationFromCursor(Cursor cursor) {
        Association association = new Association();
        association.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
        association.setName(cursor.getString(cursor.getColumnIndexOrThrow("association_name")));
        association.setFounderId(cursor.getInt(cursor.getColumnIndexOrThrow("founder_id")));
        association.setCategory(cursor.getString(cursor.getColumnIndexOrThrow("category")));
        association.setDescription(cursor.getString(cursor.getColumnIndexOrThrow("description")));
        association.setLogo(cursor.getString(cursor.getColumnIndexOrThrow("logo")));
        association.setContactPhone(cursor.getString(cursor.getColumnIndexOrThrow("contact_phone")));
        association.setContactEmail(cursor.getString(cursor.getColumnIndexOrThrow("contact_email")));
        association.setAuditStatus(cursor.getInt(cursor.getColumnIndexOrThrow("audit_status")));
        association.setAuditTime(cursor.getString(cursor.getColumnIndexOrThrow("audit_time")));
        association.setAuditAdminId(cursor.getInt(cursor.getColumnIndexOrThrow("audit_admin_id")));
        association.setAuditRemark(cursor.getString(cursor.getColumnIndexOrThrow("audit_remark")));
        association.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow("create_time")));
        association.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow("update_time")));
        association.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
        association.setMemberCount(cursor.getInt(cursor.getColumnIndexOrThrow("member_count")));
        return association;
    }
}